package com.seqb.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.seqb.common.ResultVo;
import com.seqb.common.utils.ShiroUtils;
import com.seqb.manage.dict.domain.DictDO;
import com.seqb.manage.dict.service.DictService;
import com.seqb.manage.guide.domain.GuideDO;
import com.seqb.manage.guide.service.GuideService;
import com.seqb.manage.paper.dao.PaperDao;
import com.seqb.manage.paper.dao.PaperQuestionDao;
import com.seqb.manage.paper.domain.PaperDO;
import com.seqb.manage.paper.domain.PaperQuestionDO;
import com.seqb.manage.subject.dao.SubjectDao;
import com.seqb.manage.subject.domain.SubjectDO;
import com.seqb.manage.subject.service.SubjectService;
import com.seqb.manage.user.domain.UserDO;
import com.seqb.server.service.PublicService;
import com.seqb.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2020/1/2
 */
@Service
public class PublicServiceImpl implements PublicService {
    @Autowired
    private PaperDao paperDao;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private DictService dictService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private PaperQuestionDao paperQuestionDao;
    @Autowired
    private GuideService guideService;

    @Override
    public ResultVo getSubjectType(Map<String, Object> params) {
        try {
            List<Map> resultData = new ArrayList<>();
            List<Map> childrenList;
            Map<String, Object> firstItem;
            Map<String, Object> secondItem;
            List<DictDO> firstTypes = dictService.getDictCache("bank_category");
            if (null != firstTypes && firstTypes.size() > 0) {
                for (DictDO firstDO : firstTypes) {
                    firstItem = new HashMap<>();
                    firstItem.put("id", firstDO.getId());
                    firstItem.put("text", firstDO.getName());
                    List<DictDO> secondTypes = dictService.getDictList(null, firstDO.getId());
                    if (null != secondTypes && secondTypes.size() > 0) {
                        childrenList = new ArrayList<>();
                        for (DictDO secondDO : secondTypes) {
                            secondItem = new HashMap<>();
                            secondItem.put("id", secondDO.getId());
                            secondItem.put("text", secondDO.getName());
                            childrenList.add(secondItem);
                        }
                        firstItem.put("children", childrenList);
                    }
                    resultData.add(firstItem);
                }
            }

            return ResultVo.ok().put("data", resultData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.error();
    }

    @Override
    public ResultVo getSubject(Map<String, Object> params) {
        try {
            Page<SubjectDO> list = subjectService.list(params);
            return ResultVo.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.error();
    }

    @Override
    public ResultVo getSubjectPaper(Map<String, Object> params) {
        try {
            UserDO user = ShiroUtils.getUser();
            //试卷类型(1-真题，2-章节，3-模拟)
            String type = "1";
            if (null != params.get("type")) {
                type = String.valueOf(params.get("type"));
            }
            Map<String, Object> subjectMap;
            List<Map<String, Object>> subjectList = new ArrayList<>();
            if (null == user) {
                //获取所有的科目
                Page<SubjectDO> page = subjectService.list(null);
                if (null != page) {
                    for (SubjectDO subject : page) {
                        subjectMap = new HashMap<>();
                        Long subjectId = subject.getId();
                        String subjectName = subject.getName();
                        //1-综合知识，2-案例分析，3-论文
                        QueryWrapper queryWrapper = new QueryWrapper();
                        queryWrapper.last("limit 4");
                        queryWrapper.eq("type", type);
                        queryWrapper.eq("subject_id", subjectId);
                        queryWrapper.eq("question_type", "1");
                        queryWrapper.orderByDesc("paper_date");
                        List zhzs = paperDao.selectList(queryWrapper);

                        queryWrapper = new QueryWrapper();
                        queryWrapper.last("limit 4");
                        queryWrapper.eq("type", type);
                        queryWrapper.eq("subject_id", subjectId);
                        queryWrapper.eq("question_type", "2");
                        queryWrapper.orderByDesc("paper_date");
                        List alfx = paperDao.selectList(queryWrapper);

                        queryWrapper = new QueryWrapper();
                        queryWrapper.last("limit 4");
                        queryWrapper.eq("type", type);
                        queryWrapper.eq("subject_id", subjectId);
                        queryWrapper.eq("question_type", "3");
                        queryWrapper.orderByDesc("paper_date");
                        List lw = paperDao.selectList(queryWrapper);

                        boolean isAddResult = (null == zhzs && null == alfx && null == lw) ||
                                (zhzs.size() == 0 && alfx.size() == 0 && lw.size() == 0);
                        if (isAddResult) {
                            continue;
                        }
                        subjectMap.put("subjectId", subjectId);
                        subjectMap.put("subjectName", subjectName);
                        subjectMap.put("zhzs", zhzs);
                        subjectMap.put("alfx", alfx);
                        subjectMap.put("lw", lw);
                        subjectList.add(subjectMap);
                    }
                }
            } else {
                //获取用户所选的科目
                if (null == params.get("subjectId")) {
                    return ResultVo.error("缺少参数！");
                }
                Long subjectId = Long.valueOf(String.valueOf(params.get("subjectId")));
                SubjectDO subject = subjectDao.selectById(subjectId);
                if (null == subject) {
                    return ResultVo.error("科目不存在！");
                }
                subjectMap = new HashMap<>();
                String subjectName = subject.getName();

                //1-综合知识，2-案例分析，3-论文
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.last("limit 4");
                queryWrapper.eq("type", type);
                queryWrapper.eq("subject_id", subjectId);
                queryWrapper.eq("question_type", "1");
                queryWrapper.orderByDesc("paper_date");
                queryWrapper.orderByDesc("id");
                List zhzs = paperDao.selectList(queryWrapper);

                queryWrapper = new QueryWrapper();
                queryWrapper.last("limit 4");
                queryWrapper.eq("type", type);
                queryWrapper.eq("subject_id", subjectId);
                queryWrapper.eq("question_type", "2");
                queryWrapper.orderByDesc("paper_date");
                List alfx = paperDao.selectList(queryWrapper);

                queryWrapper = new QueryWrapper();
                queryWrapper.last("limit 4");
                queryWrapper.eq("type", type);
                queryWrapper.eq("subject_id", subjectId);
                queryWrapper.eq("question_type", "3");
                queryWrapper.orderByDesc("paper_date");
                List lw = paperDao.selectList(queryWrapper);

                boolean isAddResult = (null == zhzs && null == alfx && null == lw) ||
                        (zhzs.size() == 0 && alfx.size() == 0 && lw.size() == 0);
                if (isAddResult) {

                } else {
                    subjectMap.put("subjectId", subjectId);
                    subjectMap.put("subjectName", subjectName);
                    subjectMap.put("zhzs", zhzs);
                    subjectMap.put("alfx", alfx);
                    subjectMap.put("lw", lw);
                    subjectList.add(subjectMap);
                }
            }
            return ResultVo.ok().put("data", subjectList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.error();
    }

    @Override
    public ResultVo getPaper(Map<String, Object> params) {
        try {
            if (null == params || null == params.get("paperId")) {
                return ResultVo.error("参数不足！");
            }
            Long paperId = Long.valueOf((String) params.get("paperId"));
            PaperDO paper = paperDao.selectById(paperId);
            if (null == paper) {
                return ResultVo.error("参数不足！");
            }
            return ResultVo.ok().put("data", paper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.error();
    }

    @Override
    public PageUtils getPaperQuestion(Map<String, Object> params) {
        try {
            if (null == params || null == params.get("paperId")) {
                return new PageUtils(null, 0);
            }
            Long paperId = Long.valueOf((String) params.get("paperId"));
            PaperDO paper = paperDao.selectById(paperId);
            if (null == paper) {
                return new PageUtils(null, 0);
            }
            if (null != params && null != params.get("pageNumber") && null != params.get("pageSize")) {
                PageHelper.startPage(Integer.valueOf(params.get("pageNumber").toString()), Integer.valueOf(params.get("pageSize").toString()));
            } else {
                PageHelper.startPage(1, Integer.MAX_VALUE);
            }
            Page<PaperQuestionDO> page = paperQuestionDao.list(params);
            return new PageUtils(page.getResult(), page.getTotal(), page.getPages());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageUtils(null, 0);
    }

    @Override
    public PageUtils getGuide(Map<String, Object> params) {
        try {
            if (null != params && null != params.get("pageNumber") && null != params.get("pageSize")) {
                PageHelper.startPage(Integer.valueOf(params.get("pageNumber").toString()), Integer.valueOf(params.get("pageSize").toString()));
            } else {
                PageHelper.startPage(1, Integer.MAX_VALUE);
            }
            Page<GuideDO> page = guideService.list(params);
            return new PageUtils(page.getResult(), page.getTotal(), page.getPages());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageUtils(null, 0);
    }

    @Override
    public ResultVo getGuideDO(Map<String, Object> params) {
        try {
            if (null == params || null == params.get("id")) {
                return ResultVo.error("参数不足！");
            }
            Long id = Long.valueOf((String) params.get("id"));
            GuideDO guideDO = guideService.get(id);
            if (null == guideDO) {
                return ResultVo.error("参数不足！");
            }
            return ResultVo.ok().put("data", guideDO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.error();
    }
}
